package com.lin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.entity.User;
import com.lin.exception.BusinessException;
import com.lin.reponse.Result;
import com.lin.reponse.ResultCode;
import com.lin.service.LoginLogService;
import com.lin.service.UserService;
import com.lin.vo.UserVO;
import com.wf.captcha.utils.CaptchaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2022-03-01
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Resource(name = "userServiceImpl")
    private UserService userService;

    @Resource(name = "loginLogServiceImpl")
    private LoginLogService loginLogService;

    @ApiOperation(value = "查询用户")
    @GetMapping("/getUsers")
    public Result getUsers(){
        List<User> users = userService.list();
        return Result.ok().data("users",users);
    }

    @ApiOperation(value = "根据ID查询用户")
    @GetMapping("/getUsersById")
    public Result getUsersById(Integer id){
        User user = userService.getById(id);
        if (user==null)
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                                        ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        return Result.ok().data("user",user);
    }


    @ApiOperation(value = "用户列表", notes = "模糊查询用户列表")
    @PostMapping("/findUserList")
    public Result findUserList(@RequestParam(value = "current", required = true, defaultValue = "1") Integer current,
                               @RequestParam(value = "size", required = true, defaultValue = "7") Integer size,
                               @RequestBody(required = false) UserVO userVO) {
        IPage<User> page = new Page<>(current,size);
        IPage<User> userPage = userService.findUserPage(page,userVO);
        return Result.ok().data("userPage",userPage);
    }

    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }

    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportUserExcel(HttpServletResponse response, @RequestBody(required = false) UserVO userVO) throws IOException {
        List<User> users = userService.findUsers(userVO);
        userService.exportUserExcel(response,users);
    }

    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestParam(name = "username",required = true) String username,
                        @RequestParam(name = "password",required = true) String password,
                        HttpServletRequest request){
        String token=userService.login(username,password);
        //登录日志(SpringAop+反射)
        loginLogService.addLoginLog(request);
        return Result.ok().data("token",token);
    }


    @ApiOperation(value = "修改用户密码")
    @PostMapping("/updUserPassWord")
    public Result updUserPassWord(@RequestParam(name = "username",required = true) String username,
                        @RequestParam(name = "password",required = true) String password
                        ){
        boolean bol=userService.upd(username,password);
        return Result.ok().data("bol",bol);
    }







}

